﻿using System;
using System.Collections.Generic;
using System.Web;
using FancyIdea.PingShui.Core.Dal;
using FancyIdea.PingShui.Core.Util;
using FancyIdea.PingShui.Core.DbHelper;
using System.Data;

namespace FancyIdea.PingShui.Core.Bll
{
    /// <summary>
    /// 征收项目品目 的摘要说明
    /// </summary>
    public class 征收项目品目 : HttpHandler
    {
        #region 征收项目
        private void SaveProject()
        {
            if (!CheckPower(CommUtil.GetValueByName(typeof(权限列表),
               权限列表.征收项目维护.ToString()).ToString()))
            {
                WriteReturnXml(-128, "无操作权限", null);
                return;
            }
            string method = GetStringValue("m");
            string 项目名称 = GetStringValue("征收项目名称");
            string 项目代码 = GetStringValue("征收项目代码");
            if (method.Equals("add"))
            {
                if (征收项目品目操作.AddProject(项目名称, 项目代码))
                {
                    WriteReturnXml(0, "", null);
                }
                else
                {
                    WriteReturnXml(-1, "添加失败", null);
                }
            }
            else if (method.Equals("edit"))
            {
                if (征收项目品目操作.UpdateProject(项目代码, 项目名称))
                {
                    WriteReturnXml(0, "", null);
                }
                else
                {
                    WriteReturnXml(-1, "修改失败", null);
                }
            }
        }

        private void ListProject()
        {
            if (!CheckPower(CommUtil.GetValueByName(typeof(权限列表),
               权限列表.征收项目维护.ToString()).ToString()))
            {
                WriteReturnXml(-128, "无操作权限", null);
                return;
            }
            SimpleQueryBuilder SqlBuilder
          = new SimpleQueryBuilder()
          {
              FieldsString = @"a.*",
              FromString = @"from 征收项目表 a ",
              PageOrderString = "order by a.征收项目代码",
              FieldsStringForCount = "count(a.征收项目代码) as count"
          };
            List<string> con = new List<string>();
            con.Add("a.征收项目代码 like '%" + GetStringValue("征收项目代码") + "%'");
            con.Add("a.征收项目名称 like '%" + GetStringValue("征收项目名称") + "%'");
            SqlBuilder.Conditions = con.ToArray();
            int page = GetInt32Value("page") < 1 ? 1 : GetInt32Value("page");
            string sql = SqlBuilder.BuildQuerySql(page, Const.dataPerPage);
            string sql1 = SqlBuilder.BuildQueryStatisticSql();
            DataTable table = CmdUtil.ExcuteDataTable(sql);
            object o = CmdUtil.ExcuteScalar(sql1);
            int count = int.Parse(o == null ? "0" : o.ToString());
            int total = count % Const.dataPerPage == 0 ?
                count / Const.dataPerPage : (count / Const.dataPerPage) + 1;
            WriteListXml(0, "成功", count, Const.dataPerPage, total, page, table);
        }

        private void DropDownProject()
        {
            DataTable table = 征收项目品目操作.GetDropDownProject();
            if (table != null && table.Rows.Count > 0)
            {
                WriteReturnXml(0, null, table);
            }
            else
            {
                WriteReturnXml(-1, "查询失败", table);
            }
        }

        private void GetProject()
        {
            string 征收项目代码 = GetStringValue("征收项目代码");
            if (string.IsNullOrEmpty(征收项目代码))
            {
                WriteReturnXml(-1, "参数传入错误", null);
                return;
            }
            DataRow row = 征收项目品目操作.GetProject(征收项目代码);
            if (row != null)
            {
                WriteReturnXml(0, "", row.Table);
            }
            else
            {
                WriteReturnXml(-1, "未查询到数据", null);
            }
        }

        private void DeleteProject()
        {
            if (!CheckPower(CommUtil.GetValueByName(typeof(权限列表),
               权限列表.征收项目维护.ToString()).ToString()))
            {
                WriteReturnXml(-128, "无操作权限", null);
                return;
            }
            string 征收项目代码 = GetStringValue("征收项目代码");
            if (string.IsNullOrEmpty(征收项目代码))
            {
                WriteReturnXml(-1, "参数传入错误", null);
                return;
            }
            if (征收项目品目操作.DeleteProject(征收项目代码))
            {
                WriteReturnXml(0, "", null);
            }
            else
            {
                WriteReturnXml(-1, "删除失败", null);
            }
        }

        private void DeleteProjectMulti()
        {
            if (!CheckPower(CommUtil.GetValueByName(typeof(权限列表),
               权限列表.征收项目维护.ToString()).ToString()))
            {
                WriteReturnXml(-128, "无操作权限", null);
                return;
            }
            string[] 征收项目代码 = GetStringsValue("征收项目代码");
            if (征收项目品目操作.DeleteProject(征收项目代码))
            {
                WriteReturnXml(0, "", null);
            }
            else
            {
                WriteReturnXml(-1, "删除失败", null);
            }
        }
        #endregion

        #region 征收品目

        private void SaveItem()
        {
            if (!CheckPower(CommUtil.GetValueByName(typeof(权限列表),
               权限列表.征收品目维护.ToString()).ToString()))
            {
                WriteReturnXml(-128, "无操作权限", null);
                return;
            }
            string method = GetStringValue("m");
            string 征收项目代码 = GetStringValue("征收项目代码");
            string 征收品目代码 = GetStringValue("征收品目代码");
            string 征收品目名称 = GetStringValue("征收品目名称");
            float 征收税率 = GetFloatValue("征收税率");
            float 设备定额 = GetFloatValue("设备定额");
            if (method.Equals("add"))
            {
                if (征收项目品目操作.AddItems(征收项目代码, 征收品目代码, 征收品目名称, 征收税率, 设备定额))
                {
                    WriteReturnXml(0, "", null);
                }
                else
                {
                    WriteReturnXml(-1, "添加失败", null);
                }
            }
            else
            {
                if (征收项目品目操作.UpdateItems(征收项目代码, 征收品目代码, 征收品目名称, 征收税率, 设备定额))
                {
                    WriteReturnXml(0, "", null);
                }
                else
                {
                    WriteReturnXml(-1, "修改失败", null);
                }
            }
        }

        private void ListItem()
        {
            if (!CheckPower(CommUtil.GetValueByName(typeof(权限列表),
               权限列表.征收品目维护.ToString()).ToString()))
            {
                WriteReturnXml(-128, "无操作权限", null);
                return;
            }
            SimpleQueryBuilder SqlBuilder
                     = new SimpleQueryBuilder()
                     {
                         FieldsString = @"a.*,b.征收项目名称",
                         FromString = @"from 征收品目表 a 
left join 征收项目表 b on b.征收项目代码=a.征收项目代码",
                         PageOrderString = "order by a.征收品目代码",
                         FieldsStringForCount = "count(a.征收品目代码) as count"
                     };
            List<string> con = new List<string>();
            con.Add("a.征收品目代码 like '%" + GetStringValue("征收品目代码") + "%'");
            con.Add("a.征收品目名称 like '%" + GetStringValue("征收品目名称") + "%'");
            SqlBuilder.Conditions = con.ToArray();
            int page = GetInt32Value("page") < 1 ? 1 : GetInt32Value("page");
            string sql = SqlBuilder.BuildQuerySql(page, Const.dataPerPage);
            string sql1 = SqlBuilder.BuildQueryStatisticSql();
            DataTable table = CmdUtil.ExcuteDataTable(sql);
            object o = CmdUtil.ExcuteScalar(sql1);
            int count = int.Parse(o == null ? "0" : o.ToString());
            int total = count % Const.dataPerPage == 0 ?
                count / Const.dataPerPage : (count / Const.dataPerPage) + 1;
            WriteListXml(0, "成功", count, Const.dataPerPage, total, page, table);
        }

        private void DropDownItem()
        {
            string 征收项目 = GetStringValue("征收项目代码");
            DataTable table = null;
            if (string.IsNullOrEmpty(征收项目))
            {
                table = 征收项目品目操作.GetDropDownItems();
            }
            else
            {
                table = 征收项目品目操作.GetDropDownItems(征收项目);
            }
            if (table != null && table.Rows.Count > 0)
            {
                WriteReturnXml(0, null, table);
            }
            else
            {
                WriteReturnXml(-1, "查询失败", table);
            }
        }

        private void GetItem()
        {
            string 征收品目代码 = GetStringValue("征收品目代码");
            if (string.IsNullOrEmpty(征收品目代码))
            {
                WriteReturnXml(-1, "参数传入错误", null);
                return;
            }
            DataRow row = 征收项目品目操作.GetItemsInfo(征收品目代码);
            if (row != null)
            {
                WriteReturnXml(0, "", row.Table);
            }
            else
            {
                WriteReturnXml(-1, "未查询到数据", null);
            }
        }

        private void DeleteItem()
        {
            if (!CheckPower(CommUtil.GetValueByName(typeof(权限列表),
               权限列表.征收品目维护.ToString()).ToString()))
            {
                WriteReturnXml(-128, "无操作权限", null);
                return;
            }
            string 征收品目代码 = GetStringValue("征收品目代码");
            if (string.IsNullOrEmpty(征收品目代码))
            {
                WriteReturnXml(-1, "参数传入错误", null);
                return;
            }
            if (征收项目品目操作.DeleteItems(征收品目代码))
            {
                WriteReturnXml(0, "", null);
            }
            else
            {
                WriteReturnXml(-1, "删除失败", null);
            }
        }

        private void DeleteItemMulti()
        {
            if (!CheckPower(CommUtil.GetValueByName(typeof(权限列表),
               权限列表.征收品目维护.ToString()).ToString()))
            {
                WriteReturnXml(-128, "无操作权限", null);
                return;
            }
            string[] 征收品目代码 = GetStringsValue("征收品目代码");
            if (征收项目品目操作.DeleteItems(征收品目代码))
            {
                WriteReturnXml(0, "", null);
            }
            else
            {
                WriteReturnXml(-1, "删除失败", null);
            }
        }
        #endregion
    }
}